<?php 

require("../include/config.php");
require("../API/php/fonctions.php");
require("../API/php/header.php");

// phpsql/mysql_getRappItemChargeCsv.php?milis=123450&dateBegin=01022013&dateEnd=28022013

//[X][0] = id_item
//[X][1] = code_user
//[X][2] = type
//[X][3] = phase
//[X][4] = total_charge

// IN obligatoire
$arrayGet = array(
    "dateBegin" => null,
    "dateEnd" => null
);

$arrayValeur = recupGet($arrayGet, $bolDecode);

if($arrayValeur["error"] == null){
	// CONNECION SQL
	$db = mysql_connect($host, $base, $mdp)  or die('Erreur de connexion '.mysql_error());

	// CONNECION BASE
	mysql_select_db($base,$db)  or die('Erreur de selection '.mysql_error());

	$sql = "SELECT UPPER(SUBSTRING(a.commentaire, LOCATE('ITEM', a.commentaire), 8)) as id_item, b.description as type, c.description as phase, SUM(a.charge) as total_charge
		FROM `tab_suivihebdo_enreg` a, tab_suivihebdo_type_activite b, tab_suivihebdo_type_phase c
		WHERE 1=1
		AND a.type = b.id
		AND a.phase = c.id
		AND a.`date_saisi` between STR_TO_DATE('".$arrayValeur["dateBegin"]."','%d%m%Y') AND STR_TO_DATE('".$arrayValeur["dateEnd"]."','%d%m%Y')
		AND a.commentaire REGEXP '^ITEM[0-9][0-9][0-9][A-Za-z]?$'
		GROUP BY SUBSTRING(a.commentaire, LOCATE('ITEM', a.commentaire), 7), a.type, a.phase
		ORDER BY id_item asc, type asc, phase asc 
	;";

	$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 
	
	// on fait une boucle qui va faire un tour pour chaque enregistrement 
	while($data = mysql_fetch_assoc($req)) 
	{ 
		$strSorti .= $data['type'].'&'.$data['id_item'].'&'.$data['phase'].'&'.$data['total_charge']
		.'&'
		."\r\n";
	}

	// DECONNECION SQL
	mysql_close();
}else{
	$strSorti = "ERROR:".$arrayValeur["error"];
}

require("../API/php/footer.php");
?>